package com.gaming.glb;

import java.util.ArrayList;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.BaseTextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.hardware.SensorManager;
import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.gaming.glb.PhysicBall.IOnPhysicBallLeaveSceneListener;
import com.gaming.glb.Enum.TextureIndex;

public class MainActivity extends BaseGameActivity implements IOnPhysicBallLeaveSceneListener {	 
  
	private static BoundCamera mBoundChaseCamera;

    private final int CAMERA_WIDTH = 480;
    private final int CAMERA_HEIGHT = 320;
    
    private Scene mScene;     

    private BitmapTextureAtlas mTextureCats;
 
    private TiledTextureRegion mCatRight;
    private TiledTextureRegion mCatLeft;    
    private static PhysicsWorld mPhysicsWorld;    
    private TextureRegion mBall;
    private TextureRegion mArrow;
    
    private LevelManager level;

	private Toast mToast;	
    
    @Override
    public Engine onLoadEngine() {
    	mToast = Toast.makeText(this, "YOU LOOSE!!", Toast.LENGTH_LONG);
    	
        mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), MainActivity.mBoundChaseCamera));        
    }
    

    @Override
    public void onLoadResources() {
       
        // Player sprite texture
        mTextureCats = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        mCatRight = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTextureCats, this, "gfx/AnimatedCatsLeftWithBall.png", 0, 0, 2, 3);
        mCatLeft = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTextureCats, this, "gfx/AnimatedCatsLeftFireBall.png", 0, 256, 2 , 3);
        mBall = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureCats, this, "gfx/simpleGreenBall.png", 128,0);       
        mArrow = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureCats, this, "gfx/arrow.png", 190,0);
        
        
        // Load the textures
        this.mEngine.getTextureManager().loadTextures(this.mTextureCats);
    }

    @Override
    public Scene onLoadScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        
        // Create physics world       
        MainActivity.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
        
        // Create the scene and register the physics world
        mScene = new Scene();
        mScene.registerUpdateHandler(MainActivity.mPhysicsWorld);
        
        //Load Game Map
        level = new LevelManager();
        
        Intent myIntent = getIntent(); // this is just for example purpose
        String levelName = myIntent.getStringExtra(LevelSelector.PARAMETER_NAME);
        
        TMXTiledMap mTMXTiledMap = level.getMap(this, this.mEngine, levelName);   
        level.attachMapLayersWithScene(mTMXTiledMap, mScene);
        level.fitCamera(mTMXTiledMap, mBoundChaseCamera);
        
        final ArrayList<BaseTextureRegion> mTexturesToBeLoaded = new ArrayList<BaseTextureRegion>();        
        mTexturesToBeLoaded.add(TextureIndex.CatLeft.ordinal(), mCatLeft);
        mTexturesToBeLoaded.add(TextureIndex.CatRight.ordinal(), mCatRight);
        mTexturesToBeLoaded.add(TextureIndex.Ball.ordinal(), mBall);
        mTexturesToBeLoaded.add(TextureIndex.Arrow.ordinal(), mArrow); 
        
        level.createPhysicDynamicObjects(mTMXTiledMap, mPhysicsWorld, mScene, mBoundChaseCamera, mTexturesToBeLoaded);
                      
        return mScene;
    }    
	
    @Override
    public void onLoadComplete() {       
    } 
   
	@Override
	public boolean onLeaveScene() {
		Intent g = new Intent(this, MainScreen.class);		
    	startActivity(g);
		mToast.show();
		return false;
	} 
}